﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Role
/// </summary>
public class Role
{
    private int role_id;

    public int Role_id
    {
        get { return role_id; }
        set { role_id = value; }
    }

    private string role_name;

    public string Role_name
    {
        get { return role_name; }
        set { role_name = value; }
    }

    private string path;

    public string Path
    {
        get { return path; }
        set { path = value; }
    }

    private string description;

    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    private int parent_id;

    public int Parent_id
    {
        get { return parent_id; }
        set { parent_id = value; }
    }

	public Role()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static DataTable GetAll()
    {
        DBHelp dbh = new DBHelp();
        DataTable dt = new DataTable();
        string cmd = "Role_GetAll";
        dt = dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
        return dt;
    }

    public static DataTable GetAllByParentID(int ParentID)
    {
        DBHelp dbh = new DBHelp();
        dbh.AddParameter("@parent_id", ParentID);
        DataTable dt = new DataTable();
        string cmd = "select * from tbl_role where parent_id = @parent_id";
        dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
        return dt;
    }

    public static Role GetInfo(int ID) 
    {
        DBHelp dbh = new DBHelp();
        Role role = new Role();
        dbh.AddParameter("@role_id", ID);
        string cmd = "select * from tbl_role where role_id = @role_id";
        SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.Text);
        if (sdr.Read())
        {
            role.role_id = sdr.GetInt32(0);
            if (!sdr.IsDBNull(1))
            {
                role.Role_name = sdr.GetString(1);
            }
            if (!sdr.IsDBNull(2))
            {
                role.Path = sdr.GetString(2);
            }
            if (!sdr.IsDBNull(3))
            {
                role.Description = sdr.GetString(3);
            }
            if (!sdr.IsDBNull(4))
            {
                role.Parent_id = sdr.GetInt32(4);
            }
        }
        return role;
    }

    public static Role GetInfoByParentID(int ID)
    {
        DBHelp dbh = new DBHelp();
        Role role = new Role();
        dbh.AddParameter("@parent_id", ID);
        string cmd = "select * from tbl_role where parent_id = @parent_id";
        SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.Text);
        if (sdr.Read())
        {
            role.role_id = sdr.GetInt32(0);
            if (!sdr.IsDBNull(1))
            {
                role.Role_name = sdr.GetString(1);
            }
            if (!sdr.IsDBNull(2))
            {
                role.Path = sdr.GetString(2);
            }
            if (!sdr.IsDBNull(3))
            {
                role.Description = sdr.GetString(3);
            }
            if (!sdr.IsDBNull(4))
            {
                role.Parent_id = sdr.GetInt32(4);
            }
        }
        return role;
    }

    public static void Insert(Role role)
    {
        DBHelp dbh = new DBHelp();
        dbh.AddParameter("@role_name", role.Role_name);
        dbh.AddParameter("@path", role.Path);
        dbh.AddParameter("@description", role.Description);
        dbh.AddParameter("@parent_id", role.Parent_id);
        string cmd = "Role_Insert";
        dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
    }

    public static void Update(Role role)
    {
        DBHelp dbh = new DBHelp();
        dbh.AddParameter("@role_id", role.Role_id);
        dbh.AddParameter("@role_name", role.Role_name);
        dbh.AddParameter("@path", role.Path);
        dbh.AddParameter("@description", role.Description);
        dbh.AddParameter("@parent_id", role.Parent_id);
        string cmd = "Role_Update";
        dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
    }

    public static void Delete(int ID)
    {
        DBHelp dbh = new DBHelp();
        dbh.AddParameter("@role_id", ID);
        string cmd = "Role_Delete";
        dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
    }

    public static DataTable GetRoleByParent(int ParentID)
    {
        DBHelp dbh = new DBHelp();
        DataTable dt = new DataTable();
        dbh.AddParameter("@parent_id", ParentID);
        string cmd = "Role_GetRoleByParent";
        dt = dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
        return dt;
    }
}
